12 #define D(x) cout << #x " is " << (x) << endl
14 typedef unsigned long long uint64
;
20 node(bool end
) : end(end
) {
24 if (left
== NULL
) left
= new node(false);
28 if (right
== NULL
) right
= new node(false);
34 inline uint64
pow2(int e
){
35 if (e
== 64) return 0;
36 return uint64(1) << e
;
39 void clean(node
* cur
){
40 if (cur
== NULL
) return;
49 Returns how many strings were counted under the tree rooted at cur, including it.
51 uint64
alreadyCounted(node
* cur
, int depth
= 0){
52 if (cur
== NULL
) return 0;
53 uint64 left
= alreadyCounted(cur
->left
, depth
+ 1);
54 uint64 right
= alreadyCounted(cur
->right
, depth
+ 1);
55 uint64 ret
= left
+ right
;
57 //count strings matched to this one
58 ret
+= pow2(m
- depth
) - left
- right
;
68 if (n
== 0 && m
== 0) break;
69 node
* root
= new node(true);
70 for (int i
=0; i
<n
; ++i
){
80 next
= cur
->getLeft();
81 }else if (buf
[j
] == '1'){
82 next
= cur
->getRight();
84 printf("Bad character %c at query %d, position %d\n", buf
[j
], i
, j
);
92 for (int i
=0; i
<k
; ++i
){
95 for (int j
=0; buf
[j
] != '*'; ++j
){
98 }else if (buf
[j
] == '1'){
102 string s
= buf
.substr(0, buf
.size()-1);
103 uint64 ans
= pow2(m
- s
.size())
104 - alreadyCounted(cur
->left
, s
.size()+1) - alreadyCounted(cur
->right
, s
.size()+1);